import os

from langchain_community.utils.openai_functions import (
    convert_pydantic_to_openai_function
)
from langchain_core.output_parsers.openai_functions import PydanticOutputFunctionsParser
from langchain_core.output_parsers.openai_tools import JsonOutputToolsParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from pydantic.v1 import validator


class Joke(BaseModel):
    @classmethod
    def getTemperature(city:str,date:str):
        """
            获取城市某日的天气情况。

            Args:
                city (str): 城市名称
                date (str): 日期
            Returns:
                json: 天气信息
        """
        return {"city":city,"date":date,"temperature":25,"weather":"sunny"}

OPENAI_API_KEY = os.environ["OPENAI_API_KEY_ZHIHU"]
OPENAI_API_BASE_ZHIHU = os.environ["OPENAI_API_BASE_ZHIHU"]
#model_tools = ChatOpenAI(model="gpt-3.5-turbo",temperature=0.5, openai_api_key=OPENAI_API_KEY,openai_api_base=OPENAI_API_BASE_ZHIHU).bind_tools([Joke.getTemperature])
#model_tools = ChatOpenAI(model="gpt-3.5-turbo",temperature=0.5, openai_api_key=OPENAI_API_KEY,openai_api_base=OPENAI_API_BASE_ZHIHU).bind_tools([Joke.getTemperature])
model_tools = ChatOpenAI(model="gpt-3.5-turbo", api_key=os.environ["OPENAI_API_KEY_ZHIHU"],base_url=os.environ["OPENAI_API_BASE_ZHIHU"]).bind_tools([Joke.getTemperature])


prompt = ChatPromptTemplate.from_messages(
    [("system", "You are helpful assistant"), ("user", "{input}")]
)

parser = JsonOutputToolsParser(return_id=True)

print(model_tools.kwargs["tools"])
chain = prompt | model_tools | parser

response = chain.invoke({"input": "天津2024年4月5日的天气怎么样？"})
print(response)
